# cost / budget
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns

# 读取CSV文件
df = pd.read_csv('PerformanceTest_ReviseTaskLevel.csv')

# 提取所需列
data = df[['Recipe', 'TaskNumber', 'Algorithm', 'BudgetUsage']]

# 将BudgetUsage列转换为数值类型
data['BudgetUsage'] = pd.to_numeric(data['BudgetUsage'], errors='coerce')

# 检查是否有转换失败的值（变为NaN）
if data['BudgetUsage'].isna().any():
    print("警告: 部分BudgetUsage值无法转换为数字，已设为NaN")
    print(f"无法转换的值数量: {data['BudgetUsage'].isna().sum()}")

# 计算每个工作流中各个算法的预算使用率
BudgetUsage_stats = data.groupby(['Recipe', 'TaskNumber', 'Algorithm'])['BudgetUsage'].mean().reset_index()
BudgetUsage_stats['BudgetUsage_Rate'] = BudgetUsage_stats['BudgetUsage'] * 100

# 设置图形样式
plt.style.use('default')
sns.set_palette("Set2")

# 获取所有工作流
recipes = data['Recipe'].unique()

# 为每个工作流创建预算使用率柱状图
for recipe in sorted(recipes):
    # 筛选当前工作流的数据
    recipe_data = BudgetUsage_stats[BudgetUsage_stats['Recipe'] == recipe]
    
    # 创建图形
    plt.figure(figsize=(10, 6))
    
    # 绘制柱状图
    ax = sns.barplot(data=recipe_data, x='TaskNumber', y='BudgetUsage_Rate', hue='Algorithm')
    
    # 设置标题和标签
    plt.title(f'Budget Usage by Recipe {recipe}', fontsize=16, fontweight='bold')
    plt.xlabel('TaskNumber', fontsize=12)
    plt.ylabel('Rate (%)', fontsize=12)
    plt.ylim(0, 130)
    # 在柱子上添加数值标签
    for container in ax.containers:
        ax.bar_label(container, fmt='%.1f%%', fontsize=9)
    
    # 添加图例
    plt.legend(title='Algorithm', bbox_to_anchor=(1.05, 1), loc='upper left')
    
    # 调整布局
    plt.tight_layout()
    
    # 显示图表
    plt.show()

# 打印统计结果
print("预算使用率统计:")
print(BudgetUsage_stats.to_string(index=False, float_format='%.2f'))

#      Recipe  TaskNumber   Algorithm  BudgetUsage  BudgetUsage_Rate
#       blast         100          CG         1.21            120.83
#       blast         100         DQN         0.90             90.41
#       blast         100    GRP-HEFT         0.72             71.84
#       blast         100 GRP-HEFT_HE         0.70             70.38
#       blast         100       HGDQN         0.87             87.35
#       blast         125          CG         1.21            120.85
#       blast         125         DQN         0.93             93.11
#       blast         125    GRP-HEFT         0.71             71.45
#       blast         125 GRP-HEFT_HE         0.71             70.54
#       blast         125       HGDQN         0.85             85.44
#       blast         150          CG         1.21            120.88
#       blast         150         DQN         0.93             93.44
#       blast         150    GRP-HEFT         0.71             71.30
#       blast         150 GRP-HEFT_HE         0.71             70.58
#       blast         150       HGDQN         0.86             86.17
#       blast         175          CG         1.21            120.88
#       blast         175         DQN         0.94             94.06
#       blast         175    GRP-HEFT         0.71             71.19
#       blast         175 GRP-HEFT_HE         0.71             70.67
#       blast         175       HGDQN         0.86             86.33
#       blast         200          CG         1.21            120.86
#       blast         200         DQN         0.93             92.79
#       blast         200    GRP-HEFT         0.71             71.19
#       blast         200 GRP-HEFT_HE         0.71             70.54
#       blast         200       HGDQN         0.87             87.05
#         bwa         125          CG         1.18            117.62
#         bwa         125         DQN         0.98             98.06
#         bwa         125    GRP-HEFT         0.95             95.27
#         bwa         125 GRP-HEFT_HE         0.63             63.23
#         bwa         125       HGDQN          NaN               NaN
#         bwa         150          CG         1.19            118.77
#         bwa         150         DQN         0.96             96.24
#         bwa         150    GRP-HEFT         0.96             96.08
#         bwa         150 GRP-HEFT_HE         0.64             64.00
#         bwa         150       HGDQN          NaN               NaN
#         bwa         175          CG         1.19            118.89
#         bwa         175         DQN          NaN               NaN
#         bwa         175    GRP-HEFT         0.95             94.64
#         bwa         175 GRP-HEFT_HE         0.64             64.35
#         bwa         175       HGDQN         1.00             99.69
#         bwa         200          CG         1.19            119.08
#         bwa         200         DQN          NaN               NaN
#         bwa         200    GRP-HEFT         0.98             97.98
#         bwa         200 GRP-HEFT_HE         0.63             63.25
#         bwa         200       HGDQN          NaN               NaN
#      cycles         100          CG         1.19            119.07
#      cycles         100         DQN         0.99             99.23
#      cycles         100    GRP-HEFT         1.00            100.00
#      cycles         100 GRP-HEFT_HE         0.70             69.75
#      cycles         125          CG         1.19            119.30
#      cycles         125         DQN         0.98             98.31
#      cycles         125    GRP-HEFT         1.00            100.00
#      cycles         125 GRP-HEFT_HE         0.69             69.40
#      cycles         150          CG         1.20            119.79
#      cycles         150         DQN         1.00             99.60
#      cycles         150    GRP-HEFT         1.00            100.00
#      cycles         150 GRP-HEFT_HE         0.68             68.06
#      cycles         175          CG         1.20            120.42
#      cycles         175         DQN          NaN               NaN
#      cycles         175    GRP-HEFT         1.00            100.00
#      cycles         175 GRP-HEFT_HE         0.67             67.11
#      cycles         200          CG         1.20            120.15
#      cycles         200         DQN          NaN               NaN
#      cycles         200    GRP-HEFT         1.00            100.00
#      cycles         200 GRP-HEFT_HE         0.67             67.10
# epigenomics         100          CG         1.19            118.50
# epigenomics         100         DQN         0.96             95.65
# epigenomics         100    GRP-HEFT         1.00            100.00
# epigenomics         100 GRP-HEFT_HE         0.37             36.50
# epigenomics         125          CG         1.19            118.89
# epigenomics         125         DQN         0.96             95.70
# epigenomics         125    GRP-HEFT         1.00            100.00
# epigenomics         125 GRP-HEFT_HE         0.38             37.79
# epigenomics         150          CG         1.19            119.16
# epigenomics         150         DQN          NaN               NaN
# epigenomics         150    GRP-HEFT         1.00            100.00
# epigenomics         150 GRP-HEFT_HE         0.38             38.36
# epigenomics         175          CG         1.20            119.63
# epigenomics         175         DQN         0.98             98.50
# epigenomics         175    GRP-HEFT         1.00            100.00
# epigenomics         175 GRP-HEFT_HE         0.45             45.32
# epigenomics         200          CG         1.20            120.38
# epigenomics         200         DQN         0.97             96.55
# epigenomics         200    GRP-HEFT         1.00            100.00
# epigenomics         200 GRP-HEFT_HE         0.46             45.58
#      genome         100          CG         1.18            117.73
#      genome         100         DQN         0.99             99.44
#      genome         100    GRP-HEFT         1.00             99.99
#      genome         100 GRP-HEFT_HE         0.35             35.23
#      genome         125          CG         1.18            118.10
#      genome         125         DQN         0.98             98.41
#      genome         125    GRP-HEFT         1.00            100.00
#      genome         125 GRP-HEFT_HE         0.39             38.73
#      genome         150          CG         1.18            118.13
#      genome         150         DQN         0.98             97.68
#      genome         150    GRP-HEFT         1.00             99.95
#      genome         150 GRP-HEFT_HE         0.34             33.54
#      genome         175          CG         1.17            117.50
#      genome         175         DQN         0.96             95.86
#      genome         175    GRP-HEFT         1.00            100.00
#      genome         175 GRP-HEFT_HE         0.41             40.94
#      genome         200          CG         1.18            118.21
#      genome         200         DQN         0.99             98.59
#      genome         200    GRP-HEFT         1.00            100.00
#      genome         200 GRP-HEFT_HE         0.36             36.24
#     montage         100          CG         1.21            121.40
#     montage         100         DQN         0.91             91.39
#     montage         100    GRP-HEFT         1.00             99.56
#     montage         100 GRP-HEFT_HE         0.65             65.04
#     montage         100       HGDQN         0.92             92.31
#     montage         125          CG         1.21            121.49
#     montage         125         DQN         0.94             93.58
#     montage         125    GRP-HEFT         0.99             99.50
#     montage         125 GRP-HEFT_HE         0.69             68.87
#     montage         125       HGDQN         0.93             92.85
#     montage         150          CG         1.21            121.50
#     montage         150         DQN         0.93             93.40
#     montage         150    GRP-HEFT         0.99             99.37
#     montage         150 GRP-HEFT_HE         0.53             52.96
#     montage         150       HGDQN         0.94             94.14
#     montage         175          CG         1.22            121.54
#     montage         175         DQN         0.93             92.67
#     montage         175    GRP-HEFT         0.99             99.39
#     montage         175 GRP-HEFT_HE         0.63             63.10
#     montage         175       HGDQN         0.92             91.51
#     montage         200          CG         1.22            121.56
#     montage         200         DQN         0.90             90.47
#     montage         200    GRP-HEFT         1.00             99.55
#     montage         200 GRP-HEFT_HE         0.54             53.68
#     montage         200       HGDQN         0.92             92.31
#      rnaseq          63          CG         0.43             43.39
#      rnaseq          63         DQN         0.58             58.47
#      rnaseq          63    GRP-HEFT         1.00            100.00
#      rnaseq          63 GRP-HEFT_HE         0.65             65.48
#      rnaseq         125          CG         0.62             61.62
#      rnaseq         125         DQN         0.63             63.04
#      rnaseq         125    GRP-HEFT         1.00            100.00
#      rnaseq         125 GRP-HEFT_HE         0.64             63.83
#      rnaseq         189          CG         0.85             84.88
#      rnaseq         189         DQN         0.56             55.92
#      rnaseq         189    GRP-HEFT         1.00            100.00
#      rnaseq         189 GRP-HEFT_HE         0.64             64.24
#  seismology         100          CG         0.93             92.64
#  seismology         100         DQN         0.80             79.58
#  seismology         100    GRP-HEFT         1.00            100.00
#  seismology         100 GRP-HEFT_HE         0.63             63.40
#  seismology         125          CG         1.02            101.76
#  seismology         125         DQN         0.89             88.57
#  seismology         125    GRP-HEFT         1.00            100.00
#  seismology         125 GRP-HEFT_HE         0.63             63.38
#  seismology         150          CG         1.09            109.42
#  seismology         150         DQN         0.99             99.40
#  seismology         150    GRP-HEFT         1.00            100.00
#  seismology         150 GRP-HEFT_HE         0.63             63.28
#  seismology         175          CG         1.00            100.38
#  seismology         175         DQN         0.86             85.73
#  seismology         175    GRP-HEFT         1.00            100.00
#  seismology         175 GRP-HEFT_HE         0.63             63.21
#  seismology         200          CG         1.02            102.30
#  seismology         200         DQN         0.89             89.44
#  seismology         200    GRP-HEFT         1.00            100.00
#  seismology         200 GRP-HEFT_HE         0.63             63.26
#       soykb         100          CG         1.19            119.18
#       soykb         100         DQN         0.97             97.24
#       soykb         100    GRP-HEFT         1.00             99.68
#       soykb         100 GRP-HEFT_HE         0.71             70.86
#       soykb         100       HGDQN         0.95             95.24
#       soykb         125          CG         1.20            119.63
#       soykb         125         DQN         0.96             96.37
#       soykb         125    GRP-HEFT         1.00             99.62
#       soykb         125 GRP-HEFT_HE         0.70             69.56
#       soykb         125       HGDQN         0.96             96.43
#       soykb         150          CG         1.19            119.14
#       soykb         150         DQN         0.97             97.40
#       soykb         150    GRP-HEFT         1.00             99.99
#       soykb         150 GRP-HEFT_HE         0.67             67.40
#       soykb         150       HGDQN         0.96             96.30
#       soykb         175          CG         1.19            119.36
#       soykb         175         DQN         0.96             96.41
#       soykb         175    GRP-HEFT         1.00             99.99
#       soykb         175 GRP-HEFT_HE         0.67             67.40
#       soykb         175       HGDQN         0.94             94.17
#       soykb         200          CG         1.19            119.29
#       soykb         200         DQN         0.96             96.30
#       soykb         200    GRP-HEFT         1.00            100.00
#       soykb         200 GRP-HEFT_HE         0.67             67.37
#       soykb         200       HGDQN         0.96             96.00
#   srasearch         100          CG         1.21            121.38
#   srasearch         100         DQN         0.93             93.02
#   srasearch         100    GRP-HEFT         1.00             99.84
#   srasearch         100 GRP-HEFT_HE         0.68             67.74
#   srasearch         125          CG         1.21            121.38
#   srasearch         125         DQN         0.95             95.04
#   srasearch         125    GRP-HEFT         1.00             99.90
#   srasearch         125 GRP-HEFT_HE         0.67             66.73
#   srasearch         150          CG         1.21            121.44
#   srasearch         150         DQN         0.95             94.91
#   srasearch         150    GRP-HEFT         1.00             99.92
#   srasearch         150 GRP-HEFT_HE         0.68             68.03
#   srasearch         175          CG         1.21            121.43
#   srasearch         175         DQN         0.94             94.19
#   srasearch         175    GRP-HEFT         1.00             99.93
#   srasearch         175 GRP-HEFT_HE         0.69             68.56
#   srasearch         200          CG         1.21            121.41
#   srasearch         200         DQN         0.94             94.17
#   srasearch         200    GRP-HEFT         1.00             99.94
#   srasearch         200 GRP-HEFT_HE         0.68             68.16